/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.richfaces.model.ArrangeableState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.GroupMemberDao;
import ar.com.greensoft.neo.model.GroupMember;
import ar.com.greensoft.neo.search.SearchInput;

/**
*
* @author Jorge E. Villaverde
* @since 1.0
*/
@Transactional
@Repository
public class JpaGroupMemberDaoImpl implements GroupMemberDao {
	
	@PersistenceContext
	private EntityManager entityManager;
	
	protected static final Logger logger = LoggerFactory.getLogger(JpaGroupMemberDaoImpl.class);

	@Override
	public GroupMember findById(Long id) throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Returning GroupMember with id: " + id);
		return entityManager.find(GroupMember.class, id);
	}

	@Override
	public List<GroupMember> findAll() throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Returning the list of GroupMembers");
		TypedQuery<GroupMember> query = entityManager.createNamedQuery(GroupMember.FIND_ALL_QUERY_NAME, GroupMember.class);	
		return query.getResultList();
	}

	@Override
	public List<GroupMember> findEntries(int startPosition, int maxResult) throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Returning the list of GroupMembers from: " + startPosition + " to " + (startPosition+maxResult));

		TypedQuery<GroupMember> query = entityManager.createNamedQuery(GroupMember.FIND_ALL_QUERY_NAME, GroupMember.class);

		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		
		return query.getResultList();
	}

	@Override
	public GroupMember merge(GroupMember persistentObject) throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Merging User: " + persistentObject);
		GroupMember mergeEntity = entityManager.merge(persistentObject);
		entityManager.flush();
		return mergeEntity; 
	}

	@Override
	public void persist(GroupMember transientObject) throws DaoException {
		entityManager.persist(transientObject);
	}

	@Override
	public void refresh(GroupMember persistentObject) throws DaoException {
		entityManager.refresh(persistentObject);
	}

	@Override
	public void remove(GroupMember persistentObject) throws DaoException {
		GroupMember entity = entityManager.merge(persistentObject);
		entityManager.remove(entity);
	}

	@Override
	public long count() throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Counting Group Members in the Repository");
		return (Long)entityManager.createNamedQuery(GroupMember.COUNT_QUERY_NAME).getSingleResult();
	}

	@Override
	public List<GroupMember> findEntriesBySearchInput(SearchInput input, int startPosition, int maxResult) throws DaoException {
		throw new UnsupportedOperationException();
	}

	@Override
	public long countBySearchInput(SearchInput input) throws DaoException {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<GroupMember> findEntries(ArrangeableState state,
			int startPosition, int maxResult) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long countByArrangeableState(ArrangeableState state)
			throws DaoException {
		// TODO Auto-generated method stub
		return 0;
	}
}